package com.jk.smartelderlycare.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.smartelderlycare.dao.SysCouponMapper;
import com.jk.smartelderlycare.dao.SysHomestayMapper;
import com.jk.smartelderlycare.dto.SysCoupon;
import com.jk.smartelderlycare.dto.SysHomestay;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.service.SysCouponService;
import com.jk.smartelderlycare.service.SysHomestayService;
import com.jk.smartelderlycare.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysCouponServiceImpl
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/28 19:55
 */
@Service
public class SysCouponServiceImpl implements SysCouponService {

    @Autowired
    private SysCouponMapper sysCouponMapper;

    @Autowired
    private SysHomestayMapper sysHomestayMapper;
    /**
     * @description: 优惠劵后端分页查询
     * @param: [sysCoupon, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysCoupon>
     * @author hhk
     * @date: 2024/11/28 19:56
     */
    @Override
    public PagedResponse<SysCoupon> findSysCouponPaginationList(SysCoupon sysCoupon, Integer pageNum, Integer pageSize) {
        Page<SysCoupon> page = new Page<>(pageNum,pageSize);
       IPage<SysCoupon> sysCouponList =  sysCouponMapper.findSysCouponPaginationList(sysCoupon, page);
        return PaginationUtil.getPagedResponse(sysCouponList.getRecords(),pageNum,pageSize,sysCouponList.getTotal());
    }
    /**
     * @description: 新增优惠劵
     * @param: [sysCoupon]
     * @return: void
     * @author hhk
     * @date: 2024/11/28 20:14
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysCoupon(SysCoupon sysCoupon) {
        //新增主表
        sysCouponMapper.insert(sysCoupon);
        //新增中间表
        HashMap<String, Object> CouponMap = new HashMap<>();
        if(null!=sysCoupon.getHomestayIds()){
            for (Long homestayId : sysCoupon.getHomestayIds()) {
                CouponMap.put("couponId",sysCoupon.getCouponId());
                CouponMap.put("homestayId",homestayId);
                sysCouponMapper.addSysCouponIdAndHomestayId(CouponMap);
            }
        }
    }
    /**
     * @description: 根据id删除一条数据
     * @param: [couponId]
     * @return: void
     * @author hhk
     * @date: 2024/11/28 20:14
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysCouponById(Long couponId) {
        //删除主表
        sysCouponMapper.deleteById(couponId);
        //删除中间表
        sysCouponMapper.deleteSysCouponIdAndHomestayId(couponId);

    }
    /**
     * @description: 根据id回显一条数据
     * @param: [couponId]
     * @return: com.jk.smartelderlycare.dto.SysCoupon
     * @author hhk
     * @date: 2024/11/28 20:14
     */
    @Override
    public SysCoupon findSysCouponById(Long couponId) {
        //根据id查询优惠劵信息
    SysCoupon sysCoupon = sysCouponMapper.findSysCouponById(couponId);
        if(null!=sysCoupon.getStrHomestay() && !"".equals(sysCoupon.getStrHomestay())){
            //2.获取StrHomestay 并分割字符串，得到一个String[]数组
            String[] HomestayArr = sysCoupon.getStrHomestay().split(",");
            //3、创建list<Long>集合
            List<Long> List = new ArrayList<>();
            //4.循环数组
            for (String HomestayId : HomestayArr) {
                List.add(Long.parseLong(HomestayId));
            }
            sysCoupon.setHomestayIds(List);
        }

        return sysCoupon;
    }
    /**
     * @description: 修改优惠劵
     * @param: [sysCoupon]
     * @return: void
     * @author hhk
     * @date: 2024/11/28 20:15
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updataSysCoupon(SysCoupon sysCoupon) {
        sysCouponMapper.updateById(sysCoupon);
        //二、修改中间表：先删后增
        //删除中间表
        sysCouponMapper.deleteSysCouponIdAndHomestayId(sysCoupon.getCouponId());

        //新增中间表
        HashMap<String, Object> CouponMap = new HashMap<>();
        if(null!=sysCoupon.getHomestayIds()){
            for (Long homestayId : sysCoupon.getHomestayIds()) {
                CouponMap.put("couponId",sysCoupon.getCouponId());
                CouponMap.put("homestayId",homestayId);
                sysCouponMapper.addSysCouponIdAndHomestayId(CouponMap);
            }
        }


    }
    /**
     * @description: 民宿信息
     * @param: []
     * @return: java.util.List<com.jk.smartelderlycare.dto.SysHomestay>
     * @author hhk
     * @date: 2024/11/29 16:28
     */
    @Override
    public List<SysHomestay> findHomestayList() {
        return sysHomestayMapper.selectList(null);
    }


}
